﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Recursos.Model;

namespace Recursos
{
    public partial class WebForm7 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            var userSecurity = ((Usuario)Session["UserLogged"]);
            if (userSecurity == null)
                Response.Redirect("~/NoAccess.aspx");
            var permisos = new PermisosModel();
            if (!Page.IsPostBack && userSecurity != null && permisos.obtenerRolUsuario(userSecurity.Cedula).Equals("Administrador"))
                BindData();

            else if (userSecurity == null || !permisos.obtenerRolUsuario(userSecurity.Cedula).Equals("Administrador"))
            {
                Response.Redirect("~/NoAccess.aspx");
            }

        }

        public void BindData()
        {
            var sede = new SedeModel();
            var sedes = sede.CargarSedes();
            GridView1.DataSource = sedes;
            GridView1.DataBind();

        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            var t = e.Row.RowType;

            if (t == DataControlRowType.DataRow)
            {
                if (this.GridView1.EditIndex >= 0 && e.Row.RowIndex == this.GridView1.EditIndex)
                {
                    var d1 = e.Row.FindControl("DropdownProvincia") as DropDownList;
                    var em1 = e.Row.DataItem as Provincia;
                    var provincia = new UbicacionesModel();
                    d1.DataSource = provincia.obtenerProvincias();
                    d1.DataBind();
                    d1.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();

                    var d2 = e.Row.FindControl("DropdownCanton") as DropDownList;
                    var em2 = e.Row.DataItem as Canton;
                    var canton = new UbicacionesModel();
                    d2.DataSource = canton.obtenerCantones(int.Parse(d1.SelectedValue));
                    d2.DataBind();
                    d2.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();

                    var d3 = e.Row.FindControl("DropdownDistrito") as DropDownList;
                    var em3 = e.Row.DataItem as Distrito;
                    var distrito = new UbicacionesModel();
                    d3.DataSource = distrito.obtenerDistrito(int.Parse(d2.SelectedValue));
                    d3.DataBind();
                    d3.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                }
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = GridView1.Rows[e.RowIndex];
            var sedemodelo = new SedeModel();
            var sedeId = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            var txtnombre = (TextBox)row.FindControl("Nombre");
            var dplprovincia = (DropDownList)row.FindControl("DropdownProvincia");
            var dplcanton = (DropDownList)row.FindControl("DropdownCanton");
            var dpldistrito = (DropDownList)row.FindControl("DropdownDistrito");
            var txtdireccion = (TextBox)row.FindControl("DireccionExacta");
            var efectuado=sedemodelo.ActualizarSede(sedeId, txtnombre.Text, txtdireccion.Text, int.Parse(dpldistrito.SelectedValue));
            GridView1.EditIndex = -1;
            BindData();
            if(efectuado==true)
                WebMsgBox.Show("Sede "+txtnombre.Text+" editada correctamente", "Modificación de Sedes");
            else
                WebMsgBox.Show("La sede "+txtnombre.Text +" esta duplicada. No se realizo la actualización", "Modificación de Sedes");
          
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = true;
            GridView1.EditIndex = -1;
            BindData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            GridViewRow row = GridView1.Rows[e.RowIndex];
            var sedeId = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            var sede = new SedeModel();
            var sedeaeliminar = sede.CargarPorId(sedeId);
            var conflicto = sede.EliminarSede(sedeId);
            GridView1.EditIndex = -1;
            BindData();
            if (conflicto == false)
                WebMsgBox.Show("Sede "+sedeaeliminar.Nombre+" eliminada correctamente", "Eliminar Sede");
            else
                WebMsgBox.Show("La sede "+sedeaeliminar.Nombre+" cuenta con espacios. No se realizo la eliminación", "Eliminar Sede");

        }

        protected void DropdownCanton_SelectedIndexChanged(object sender, EventArgs e)
        {
            var dplcanton = (DropDownList)GridView1.Rows[GridView1.EditIndex].FindControl("DropdownCanton");
            var dpldistrito = (DropDownList)GridView1.Rows[GridView1.EditIndex].FindControl("DropdownDistrito");
            var canton = new UbicacionesModel();
            dpldistrito.DataSource = canton.obtenerDistrito(int.Parse(dplcanton.SelectedValue));
            dpldistrito.DataBind();
        }

        protected void DropdownProvincia_SelectedIndexChanged(object sender, EventArgs e)
        {


            var dplprovincia = (DropDownList)GridView1.Rows[GridView1.EditIndex].FindControl("DropdownProvincia");


            var dplcanton = (DropDownList)GridView1.Rows[GridView1.EditIndex].FindControl("DropdownCanton");
            var dpldistrito = (DropDownList)GridView1.Rows[GridView1.EditIndex].FindControl("DropdownDistrito");
            
            var canton = new UbicacionesModel();
            dplcanton.DataSource = canton.obtenerCantones(int.Parse(dplprovincia.SelectedValue));
            dplcanton.DataBind();
            dpldistrito.DataSource = canton.obtenerDistrito(int.Parse(dplcanton.SelectedValue));
            dpldistrito.DataBind();
     }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
          // GridView1.SelectedRow
        }
    }
}